package com.ruoyi.business.service.impl;

import com.ruoyi.business.mapper.WsdNotificationsMapper;
import com.ruoyi.business.service.IWsdNotificationsService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.WsdNotifications;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 存储通知的基本信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-19
 */
@Service
@Slf4j
public class WsdNotificationsServiceImpl implements IWsdNotificationsService
{
    @Autowired
    private WsdNotificationsMapper wsdNotificationsMapper;

    /**
     * 查询存储通知的基本信息
     * 
     * @param notificationsId 存储通知的基本信息主键
     * @return 存储通知的基本信息
     */
    @Override
    public WsdNotifications selectWsdNotificationsByNotificationsId(Long notificationsId) {

        return wsdNotificationsMapper.selectWsdNotificationsByNotificationsId(notificationsId);
    }

    /**
     * 查询存储通知的基本信息列表
     * 
     * @param wsdNotifications 存储通知的基本信息
     * @return 存储通知的基本信息
     */
    @Override
    public List<WsdNotifications> selectWsdNotificationsList(WsdNotifications wsdNotifications) {
        SysUser user = getLoginUser().getUser();
        wsdNotifications.setSchoolId(user.getSchoolId());
        List<WsdNotifications> wsdNotificationsArrayList = new ArrayList<>();
        Boolean inform = wsdNotifications.getInform();
        //true表示当前通知，false表示历史通知
        if (Boolean.FALSE.equals(inform)){
            List<WsdNotifications> List =  wsdNotificationsMapper.historicalNotice(wsdNotifications);
            wsdNotificationsArrayList = List;
        }else {
            wsdNotifications.setStatus(Constants.STATUS);
            List<WsdNotifications> List =  wsdNotificationsMapper.selectWsdNotificationsList(wsdNotifications);
            wsdNotificationsArrayList = List;
        }
       return wsdNotificationsArrayList;
    }

    /**
     * 新增存储通知的基本信息
     * 
     * @param wsdNotifications 存储通知的基本信息
     * @return 结果
     */
    @Override
    public int insertWsdNotifications(WsdNotifications wsdNotifications) {
        return wsdNotificationsMapper.insertWsdNotifications(wsdNotifications);
    }

    /**
     * 修改存储通知的基本信息
     * 
     * @param wsdNotifications 存储通知的基本信息
     * @return 结果
     */
    @Override
    public int updateWsdNotifications(WsdNotifications wsdNotifications)
    {
        return wsdNotificationsMapper.updateWsdNotifications(wsdNotifications);
    }

    /**
     * 批量删除存储通知的基本信息
     *
     * @param notificationsIds 需要删除的存储通知的基本信息主键
     * @param schoolId
     * @return 结果
     */
    @Override
    public int deleteWsdNotificationsByNotificationsIds(Long[] notificationsIds, Long schoolId) {

        return wsdNotificationsMapper.deleteWsdNotificationsByNotificationsIds(notificationsIds,schoolId);
    }

    /**
     * 删除存储通知的基本信息信息
     * 
     * @param notificationsId 存储通知的基本信息主键
     * @return 结果
     */
    @Override
    public int deleteWsdNotificationsByNotificationsId(Long notificationsId)
    {
        return wsdNotificationsMapper.deleteWsdNotificationsByNotificationsId(notificationsId);
    }

    /**
     * 定时扫描过期的通知
     *
     * @return
     */
    @Override
    @Transactional
    public Boolean updateNotificationStatus() {
        try {
            log.info("定时任务开启");
            LocalDate today = LocalDate.now();  // 获取今天的日期
            int updatedCount = wsdNotificationsMapper.updateNotificationStatus(today);
            System.out.println("更新成功" + updatedCount);
           log.info("更新成功");
            return updatedCount > 0; // 如果更新了一条或多条记录，返回true
        } catch (Exception e) {
            log.error("更新失败" + e);
            throw new RuntimeException(e);
        }
    }
}
